package com.wudi.estate.controller.assetmanage;

import com.sun.org.apache.xpath.internal.operations.Mod;
import com.wudi.estate.bean.assetmanage.Asset;
import com.wudi.estate.bean.assetmanage.Device;
import com.wudi.estate.service.assetmanage.AssetService;
import com.wudi.estate.service.assetmanage.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import java.net.Socket;
import java.util.List;
import java.util.Map;

@SessionAttributes(value = "maps")
@Controller
@RequestMapping("/asset")
public class AssetController {
    @Autowired
    private AssetService assetService;
    @Autowired
    DeviceService deviceService;
    @RequestMapping("/listUi")
    public String listUi(ModelMap map){
        try {
            Map<String,Object> maps = assetService.selectDeviceAndCommunityAddUi();
            map.addAttribute("maps",maps);
            List<Map<String ,Object>> lists = assetService.selectDeviceAndCommunity();
            map.addAttribute("lists",lists);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "asset_list";
    }
    @RequestMapping("/addUi")
    public String addUi(){
        return "asset_add";
    }
    @RequestMapping("/add")
    public String add(Asset asset){
        System.out.println(asset);
        try {
            assetService.insert(asset);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/asset/listUi";
    }
    @RequestMapping("/editUi")
    public String editUi(long id,ModelMap map){
        System.out.println(id);
        Asset asset = assetService.selectById(id);
        System.out.println(asset);
        map.put("asset",asset);
        return "asset_edit";
    }
    @RequestMapping("/edit")
    public String edit(Asset asset){
        System.out.println(asset);
       assetService.update(asset);
        return "redirect:/asset/listUi";
    }
    @RequestMapping("/delete")
    public String delete(long id){
        assetService.delete(id);
        return "redirect:/asset/listUi";
    }
}
